import DC from '@dvgis/dc-sdk/dist/dc.base.min' //基础包
import DcCore from '@dvgis/dc-sdk/dist/dc.core.min' //核心包
// import DcChart from '@dvgis/dc-sdk/dist/dc.chart.min' //chart包
// import DcMapv from '@dvgis/dc-sdk/dist/dc.mapv.min' //mapv包
// import DcS3M from '@dvgis/dc-sdk/dist/dc.s3m.min' //s3m包
import '../static/style.css'
import '@dvgis/dc-sdk/dist/dc.core.min.css' // 主要样式
global.DC = DC //将DC提升到全局变量，方便在工程中直接使用
DC.use(DcCore) //安装DC核心库
// DC.use(DcChart) //安装DC图标库，使用前确保echarts为全局函数
// DC.use(DcMapv) //安装Mapv库
//使用CDN方式时无需以上代码,会自动安装
let viewer = undefined;
let szData = undefined
let sz_out_data = undefined
let sz_zxd_data = [];
import $ from 'jquery'
DC.ready(() => {
  viewer = new DC.Viewer('viewer-container',{
    orderIndependentTranslucency: false,
    contextOptions: {
      webgl: {
        alpha: true
      }
    }
  });
  
  // let baseLayer = new DC.ImageryLayerFactory.createAmapImageryLayer({
  //   style:'img',
  //   crs:"WGS84"
  // })
  // viewer.addBaseLayer(baseLayer);

  viewer.setOptions({
    showMoon: false,
    showSun: false,
    enableFaxx: true,
    globe: {
      show: false
    },
    skyBox: {
      show: false
    }
  })
  viewer.use(new DC.Effect());
  // viewer.effect.night.enable = true
  // viewer.effect.bloom.brightness = 0.1

  loadVLine()
  viewer.zoomToPosition(
    // '120.6119943479279,30.237286794313203,126132.920,0,-48.84',
    '117.005343,36.667922,296132.920,0,-90',
    () => {
      loadSzData()
      loadSzOutline()
      loadSzPolygon()
      loadSzLine()
      loadSzZxd()
    }
  )
})


function loadVLine() {
  let layer = new DC.PrimitiveLayer('vLine_layer')
  viewer.addLayer(layer)
  let positions = [
    { lng: 119.61305431791536, lat: 31.949257981346843 },
    { lng: 119.66748528834533, lat: 31.569088949848066 },
    { lng: 119.93621458239951, lat: 30.862978381864128 },
    { lng: 120.33269379413352, lat: 30.779032494324515 },
    { lng: 120.91846175667051, lat: 30.772412670398687 },
    { lng: 121.37081537125513, lat: 31.031108498202467 },
    { lng: 121.68788865110909, lat: 31.59956942613548 },
    { lng: 121.31821229420665, lat: 32.00645809208013 }
  ]
  positions.forEach(item => {
    let position = DC.Position.fromObject(item)
    let end = position.copy()
    end.alt = 80000 * Math.random()
    let line = new DC.FlowLinePrimitive([position, end], 0.5)
    line.setStyle({
      speed: 8,
      color: DC.Color.fromCssColorString('rgb(240,131,61)'),
      percent: 0.2,
      gradient: 0.01
    })
    layer.addOverlay(line)
  })
}
function loadSzData() {
  // szData = new DC.GeoJsonLayer('sz', 'sz.geojson')
  // sz_out_data = new DC.GeoJsonLayer('sz-out', 'sz_out.geojson')
  szData = new DC.GeoJsonLayer('sz', 'xian_3701_org.geojson')
  sz_out_data = new DC.GeoJsonLayer('sz-out', 'shi_3701.geojson')
}
async function  loadSzZxd() {
  let layer_sz_zxd = new DC.VectorLayer('layer-sz-zxd').addTo(viewer)
  let geo = await $.getJSON('xian_3701_org.geojson');
// console.log(turf.centroid(geo.features[0]))
const { turf } = DC.Namespace;
sz_zxd_data = geo.features.map(ft=>({label:ft.properties.name,coord:turf.centroid(ft).geometry.coordinates[0]+','+turf.centroid(ft).geometry.coordinates[1]}));
  console.log(sz_zxd_data)
  sz_zxd_data.forEach(item => {
    let position = DC.Position.fromString(item.coord)
    position.alt = 3000
    let billboard = new DC.Billboard(
      position,
      'bg_name.png'
    )
    billboard.size = [item.label.length * 25, 32]
    billboard.setStyle({
      scale: 0.8,
      distanceDisplayCondition: {
        near: 0,
        far: 2.5e5
      }
    })
    billboard.setLabel(item.label, {
      font: '10px',
      disableDepthTestDistance: Number.POSITIVE_INFINITY,
      distanceDisplayCondition: {
        near: 0,
        far: 2.5e5
      }
    })
    let circle = new DC.Circle(position, 2000)
    circle.setStyle({
      height: 2000,
      material: new DC.CircleWaveMaterialProperty({
        speed: 5,
        color: DC.Color.fromCssColorString('rgb(205,100,33)')
      })
    })
    layer_sz_zxd.addOverlay(billboard).addOverlay(circle)
  })
}

function loadSzPolygon() {
  viewer.addLayer(sz_out_data)
  sz_out_data.eachOverlay(entity => {
    DC.Util.merge(entity.polygon, {
      outline: false,
      material: DC.Color.fromCssColorString('rgb(47,50,54)'),
      extrudedHeight: 2000
    })
  })
}

function loadSzOutline() {
  let layer_sz_outline = new DC.VectorLayer('layer-sz-outline').addTo(
    viewer
  )
  let now = DC.JulianDate.now()
  sz_out_data?.eachOverlay(entity => {
    if (entity.polygon) {
      let positions = DC.Transform.transformCartesianArrayToWGS84Array(
        entity.polygon.hierarchy.getValue(now).positions
      )
      positions.map(item => {
        item.alt = 2000
      })
      let polyline = new DC.Polyline(positions)
      polyline.setStyle({
        width: 2,
        material: new DC.PolylineTrailMaterialProperty({
          speed: 2,
          color: DC.Color.fromCssColorString('rgb(149,236,217)')
        })
      })
      layer_sz_outline.addOverlay(polyline)
    }
  })
}

function loadSzLine() {
  let layer_sz = new DC.VectorLayer('layer-sz-line').addTo(viewer)
  let now = DC.JulianDate.now()
  szData.eachOverlay(entity => {
    if (entity.polygon) {
      let positions = DC.Transform.transformCartesianArrayToWGS84Array(
        entity.polygon.hierarchy.getValue(now).positions
      )
      positions.map(item => {
        item.alt = 2000
      })
      let polyline = new DC.Polyline(positions)
      polyline.setStyle({
        width: 1,
        material: new DC.PolylineFlickerMaterialProperty({
          speed: 10 * Math.random(),
          color: DC.Color.fromCssColorString('rgb(116,173,250)')
        })
      })
      layer_sz.addOverlay(polyline)
    }
  })
}